//
//  WBVisitorView.swift
//  weibobo
//
//  Created by KIMI on 16/9/20.
//  Copyright © 2016年 KIMI. All rights reserved.
//

import UIKit
//设置代理
protocol WBVisitorViewDelegate : NSObjectProtocol{
    
    //代理方法 
    func didSelectedLogin()
}

class WBVisitorView: UIView {
    
    //创建按钮点击回调的闭包
    var loginClosure: (() -> ())?
    
    //定义代理对象 weak修饰
    weak var delegate: WBVisitorViewDelegate?
    
    //懒加载创建旋转视图
    private lazy var cycleImgV: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //添加中间icon
    private lazy var iconImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //添加罩层
    private lazy var maskImageV: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //添加信息标签
    private lazy var meassageLbl: UILabel = {
        
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFont(ofSize: 13)
        label.numberOfLines = 0
        label.textColor = UIColor.darkGray
        label.textAlignment = .center
        
        return label
        
    }()
    //添加登陆按钮
    private lazy var loginBtn: UIButton = {
       
        let button = UIButton()
        
        //登陆按钮点击事件
        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        
        button.setTitle("登录", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        //取消按钮选中高亮状态
        button.adjustsImageWhenHighlighted = false
        return button
    }()
    
    //添加注册按钮
    private lazy var registerBtn: UIButton = {
        
        let button = UIButton()
        
        //注册按钮点击事件
        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        
        button.setTitle("注册", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        //取消按钮选中高亮状态
        button.adjustsImageWhenHighlighted = false
        return button
    }()
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        
        //  设置背景色
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        addSubview(cycleImgV)
        addSubview(maskImageV)
        addSubview(iconImage)
        addSubview(meassageLbl)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        cycleImgV.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        maskImageV.snp_makeConstraints { (make) in
            
            make.center.equalTo(cycleImgV)
        }
        iconImage.snp_makeConstraints { (make) in
            
            make.center.equalTo(cycleImgV)
        }
        meassageLbl.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(cycleImgV)
            make.top.equalTo(cycleImgV.snp_bottom)
            make.width.equalTo(224)
        }
        loginBtn.snp_makeConstraints { (make) in
            
            make.leading.equalTo(meassageLbl)
            make.top.equalTo(meassageLbl.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        
        registerBtn.snp_makeConstraints { (make) in
            
            make.trailing.equalTo(meassageLbl)
            make.top.equalTo(loginBtn)
            make.size.equalTo(loginBtn)
        }
    }
    
    //抽取一个方法修改访客视图信息
    func updateVisitorInfo(messageTitle: String?, imageName: String?){
        
        if let messageT = messageTitle, let name = imageName{
            //代表是信息,发现,我的视图
            meassageLbl.text = messageT
            iconImage.image = UIImage(named: name)
            //隐藏旋转视图
            cycleImgV.isHidden = true
        
        }else{
            
            starAnimaton()
        }
    }
    
    //开启旋转视图动画
    private func starAnimaton() {
        //创建旋转的核心动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
       //设置动画目的地
        animation.toValue = M_PI * 2
        //!!!!设置动画时长
        animation.duration = 15
        //设置重复次数
        animation.repeatCount = MAXFLOAT
        //动画失去焦点让其不释放
        animation.isRemovedOnCompletion = false
        //添加动画
        cycleImgV.layer.add(animation, forKey: nil)
    }
    
    //登陆/注册 按钮点击事件
    @objc private func loginAction() {
        
        loginClosure!()
        //执行代理方法
        delegate?.didSelectedLogin()
    }
}
